package com.ruoyi.web.controller.common;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import com.lark.oapi.Client;
import com.lark.oapi.core.request.RequestOptions;
import com.lark.oapi.core.response.RawResponse;
import com.lark.oapi.core.token.AccessTokenType;
import com.ruoyi.GetToken;
import com.ruoyi.common.core.domain.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author linziheng
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/imagesUpload")
public class ImagesUploadController {

    /**
     * 上传图片获取路径
     */
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file) throws IOException {
        if (ObjectUtil.isNull(file)) {
            return R.fail("上传文件不能为空");
        }
        String fileName =  file.getOriginalFilename();
        String filePath = "/home/img/" + fileName;
//        String filePath = "/Users/linziheng/Desktop/log/" + fileName;
//        String filePath = "D://" + fileName;
        File dest = new File(filePath);
        file.transferTo(dest);
        Map<String, String> map = new HashMap<>(2);
        map.put("url", "http://8.138.134.68:9001/"+fileName);
        map.put("fileName",fileName);
        map.put("ossId",fileName);
        return R.ok("上传成功",map);
    }

    @PostMapping("/feishuUpload")
    public R feishuUpload(@RequestParam("file") MultipartFile file) throws Exception {
        if (ObjectUtil.isNull(file)) {
            return R.fail("上传文件不能为空");
        }
        String fileName =  file.getOriginalFilename();
        GetToken getToken = new GetToken();
        String tenantAccessToken = getToken.getTenantAccessTokenBySelfBuiltApp();
        String appId = getToken.appId;
        String appSecret = getToken.appSecret;
        Client client = Client.newBuilder(appId, appSecret).build();

        Image body = new Image();
        body.setContent(convertMultipartFileToFile(file));
        body.setType("image");
        body.setName(fileName);
        RequestOptions opt = RequestOptions.newBuilder().supportUpload().build();
        RawResponse resp = client.post(
            "https://open.feishu.cn/open-apis/im/v1/images"
            , body
            , AccessTokenType.Tenant
            , opt);

        System.out.println(new String(resp.getBody()));

        return R.ok("上传成功");
    }
    public static class Image {
        @SerializedName("name")
        private String name;
        @SerializedName("type")
        private String type;
        @SerializedName("content")
        private File content;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public File getContent() {
            return content;
        }

        public void setContent(File content) {
            this.content = content;
        }
    }

    public static File convertMultipartFileToFile(MultipartFile file) throws IOException {
        File convFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            System.out.println("File is empty or its size is zero, please check!");
        } else {
            InputStream inputStream = file.getInputStream();
            convFile = new File(file.getOriginalFilename());
            FileOutputStream fos = new FileOutputStream(convFile);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
            fos.close();
            inputStream.close();
        }
        return convFile;
    }
}
